﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DVSProject.Lib;
using DVSProject.Models;

namespace DVSProject.Controllers
{
    public class LocationMachineSetController : Controller
    {
        //
        // GET: /LocationMachineSet/

        public ActionResult Index()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationMachineSetRepo _LocationMachineSetRepo = new LocationMachineSetRepo();
            IEnumerable<LocationMachineSetModels> _LocationMachineSets = _LocationMachineSetRepo.GetLocationMachineSet();
            return View(_LocationMachineSets);
        }

        //
        // GET: /LocationMachineSet/Details/5

        public ActionResult Details(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationMachineSetRepo _LocationMachineSetRepo = new LocationMachineSetRepo();
            TransLocationMachineSetModels TransLocationMachineSet = new TransLocationMachineSetModels();
            TransLocationMachineSet = _LocationMachineSetRepo.GetLocationMachineSetById(id);
            return View(TransLocationMachineSet);
        }

        //
        // GET: /LocationMachineSet/Create

        public ActionResult Create()
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationMachineSetModels LocationMachineSet = new LocationMachineSetModels();

            LocationSetRepo _LocationSetRepo = new LocationSetRepo();
            IEnumerable<LocationSetModels> _LocationSets = _LocationSetRepo.GetLocationSet();
            string selectedId = "M001";
            IEnumerable<SelectListItem> selectlist = from s in _LocationSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.LocationSetCode == selectedId),
                                                         Text = s.LocationSetCode +'-'+ s.LocSetNameTh,
                                                         Value = s.LocationSetCode.ToString()
                                                     };

            MachineRepo _MachineRepo = new MachineRepo();
            IEnumerable<MachineModels> _MachineSets = _MachineRepo.GetMachineForNewSetup();
            string  selectedMachinId = "1";
            IEnumerable<SelectListItem> MachineSelect = from s in _MachineSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.SerialNo == selectedMachinId),
                                                         Text = s.SerialNo + '<' + s.Model_NameTh + '>' +s.NameTh,
                                                         Value = s.SerialNo.ToString()
                                                     };

            LocationMachineSet.MachineSelect = MachineSelect;
            LocationMachineSet.LocSetSelect = selectlist;
            return View(LocationMachineSet);
        } 

        //
        // POST: /LocationMachineSet/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
               
                // TODO: Add insert logic here
                LocationMachineSetRepo _LocationMachineSetRepo = new LocationMachineSetRepo();
                if (_LocationMachineSetRepo.ExistsDupSerialNo(collection["SerialNo"]))
                {
                    //Dub resial no
                }
                else
                {
                    DateTime _getdate = SF.GetDateServer();
                    LocationMachineSetModels LocationMachineSet = new LocationMachineSetModels();

                    // LocationMachineSet.code_no = collection["code_no"];
                    LocationMachineSet.LocationSetCode = collection["LocationSetCode"];
                    LocationMachineSet.SerialNo = collection["SerialNo"];
                    LocationMachineSet.start_meter_no = (!String.IsNullOrEmpty(collection["start_meter_no"])) ? SF.GetDouble(collection["start_meter_no"]) : 0;
                    LocationMachineSet.last_meter_no = (!String.IsNullOrEmpty(collection["last_meter_no"])) ? SF.GetDouble(collection["last_meter_no"]) : 0;
                    LocationMachineSet.total_meter_value = (!String.IsNullOrEmpty(collection["total_meter_value"]))? SF.GetDouble(collection["total_meter_value"]):0;
                    LocationMachineSet.contract_start_date = (!String.IsNullOrEmpty(collection["contract_start_date"])) ? collection["contract_start_date"] : _getdate.ToString("dd-MM-yyyy", new System.Globalization.CultureInfo("en-US"));
                    LocationMachineSet.remark = collection["remark"];
                    LocationMachineSet.CreateBy = (string)Session["User"];

                    bool result = _LocationMachineSetRepo.Create(LocationMachineSet);
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /LocationMachineSet/Edit/5
 
        public ActionResult Edit(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationMachineSetRepo _LocationMachineSetRepo = new LocationMachineSetRepo();
            TransLocationMachineSetModels TransLocationMachineSet = new TransLocationMachineSetModels();

            TransLocationMachineSet = _LocationMachineSetRepo.GetLocationMachineSetById(id);
            LocationSetRepo _LocationSetRepo = new LocationSetRepo();
            IEnumerable<LocationSetModels> _LocationSets = _LocationSetRepo.GetLocationSet();
            string selectedId = "M001";
            IEnumerable<SelectListItem> selectlist = from s in _LocationSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.LocationSetCode == selectedId),
                                                         Text = s.LocationSetCode + '-' + s.LocSetNameTh,
                                                         Value = s.LocationSetCode.ToString()
                                                     };

            MachineRepo _MachineRepo = new MachineRepo();
            IEnumerable<MachineModels> _MachineSets = _MachineRepo.GetMachineForNewSetup();
            string selectedMachinId = "1";
            IEnumerable<SelectListItem> MachineSelect = from s in _MachineSets
                                                        select new SelectListItem
                                                        {
                                                            Selected = (s.SerialNo == selectedMachinId),
                                                            Text = s.SerialNo + '<' + s.Model_NameTh + '>' + s.NameTh,
                                                            Value = s.SerialNo.ToString()
                                                        };
            IEnumerable<MachineModels> _MachineSetShows = _MachineRepo.GetMachineForChange();
            string _selectedMachinId = "1";
            IEnumerable<SelectListItem> refMachineSelect = from s in _MachineSetShows
                                                        select new SelectListItem
                                                        {
                                                            Selected = (s.SerialNo == _selectedMachinId),
                                                            Text = s.SerialNo + '<' + s.Model_NameTh + '>' + s.NameTh,
                                                            Value = s.SerialNo.ToString()
                                                        };

            TransLocationMachineSet.tmpLocSetSelect = selectlist;
            TransLocationMachineSet.tmpMachineSelect = MachineSelect;
            TransLocationMachineSet.refLocSetSelect = selectlist;
            TransLocationMachineSet.refMachineSelect = refMachineSelect;
            return View(TransLocationMachineSet);
        }

        //
        // POST: /LocationMachineSet/Edit/5

        [HttpPost]
        public ActionResult Edit(string  id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here

                LocationMachineSetRepo _LocationMachineSetRepo = new LocationMachineSetRepo();
                TransLocationMachineSetModels TransLocationMachineSet = new TransLocationMachineSetModels();
                TransLocationMachineSet.tmp_code_no = id;
                TransLocationMachineSet.tmp_LocationSetCode = collection["tmp_LocationSetCode"];
                TransLocationMachineSet.tmp_SerialNo = collection["tmp_SerialNo"];
                TransLocationMachineSet.tmp_start_meter_no = (!String.IsNullOrEmpty(collection["tmp_start_meter_no"])) ? SF.GetDouble(collection["tmp_start_meter_no"]) : 0;
                TransLocationMachineSet.tmp_last_meter_no = (!String.IsNullOrEmpty(collection["tmp_last_meter_no"])) ? SF.GetDouble(collection["tmp_last_meter_no"]) : 0;
                TransLocationMachineSet.tmp_total_meter_value = (!String.IsNullOrEmpty(collection["tmp_total_meter_value"])) ? SF.GetDouble(collection["tmp_total_meter_value"]) : 0;
                TransLocationMachineSet.ref_SerialNo = collection["ref_SerialNo"];
                TransLocationMachineSet.ref_start_meter_no = (!String.IsNullOrEmpty(collection["ref_start_meter_no"])) ? SF.GetDouble(collection["ref_start_meter_no"]) : 0;
                TransLocationMachineSet.ref_total_meter_value = (!String.IsNullOrEmpty(collection["ref_total_meter_value"])) ? SF.GetDouble(collection["ref_total_meter_value"]) : 0;
                TransLocationMachineSet.ref_start_meter_no = (!String.IsNullOrEmpty(collection["ref_total_meter_value"])) ? SF.GetDouble(collection["ref_total_meter_value"]) : 0;
                TransLocationMachineSet.ref_CreateBy = (!String.IsNullOrEmpty(collection["ref_CreateBy"])) ? (String)collection["ref_CreateBy"] : "";
                TransLocationMachineSet.ref_total_meter_value = (!String.IsNullOrEmpty(collection["ref_total_meter_value"])) ? SF.GetDouble(collection["ref_total_meter_value"]) : 0;
                //TransLocationMachineSet.ref_CreateOn = collection["ref_CreateOn"];
                TransLocationMachineSet.remarks = collection["remarks"];
                TransLocationMachineSet.tmp_CreateBy = (string)Session["User"];
                
                //ref_SerialNo 
                bool result = _LocationMachineSetRepo.Edit(TransLocationMachineSet);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: /LocationMachineSet/Edit/5

        public ActionResult EditLms(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationMachineSetRepo _LocationMachineSetRepo = new LocationMachineSetRepo();
            LocationMachineSetModels LocationMachineSet = new LocationMachineSetModels();

            LocationMachineSet = _LocationMachineSetRepo.GetLocationMachineSetForEditById(id);
            LocationSetRepo _LocationSetRepo = new LocationSetRepo();
            IEnumerable<LocationSetModels> _LocationSets = _LocationSetRepo.GetLocationSet();
            string selectedId = "M004";
            IEnumerable<SelectListItem> selectlist = from s in _LocationSets
                                                     select new SelectListItem
                                                     {
                                                         Selected = (s.LocationSetCode == selectedId),
                                                         Text = s.LocationSetCode + '-' + s.LocSetNameTh,
                                                         Value = s.LocationSetCode.ToString()
                                                     };

            MachineRepo _MachineRepo = new MachineRepo();
            //IEnumerable<MachineModels> _MachineSets = _MachineRepo.GetMachineForNewSetup();
            //string selectedMachinId = "1";
            //IEnumerable<SelectListItem> MachineSelect = from s in _MachineSets
            //                                            select new SelectListItem
            //                                            {
            //                                                Selected = (s.SerialNo == selectedMachinId),
            //                                                Text = s.SerialNo + '<' + s.Model_NameTh + '>' + s.NameTh,
            //                                                Value = s.SerialNo.ToString()
            //                                            };
            IEnumerable<MachineModels> _MachineSetShows = _MachineRepo.GetMachine();
            string _selectedMachinId = "1";
            IEnumerable<SelectListItem> refMachineSelect = from s in _MachineSetShows
                                                           select new SelectListItem
                                                           {
                                                               Selected = (s.SerialNo == _selectedMachinId),
                                                               Text = s.SerialNo + '<' + s.Model_NameTh + '>' + s.NameTh,
                                                               Value = s.SerialNo.ToString()
                                                           };

            LocationMachineSet.LocSetSelect = selectlist;
            LocationMachineSet.MachineSelect = refMachineSelect;
            //TransLocationMachineSet.refLocSetSelect = selectlist;
            //TransLocationMachineSet.refMachineSelect = refMachineSelect;
            return View(LocationMachineSet);
        }

        //
        // POST: /LocationMachineSet/Edit/5

        [HttpPost]
        public ActionResult EditLms(string id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add update logic here
                DateTime _getdate = SF.GetDateServer();
                LocationMachineSetRepo _LocationMachineSetRepo = new LocationMachineSetRepo();
                LocationMachineSetModels LocationMachineSet = new LocationMachineSetModels();
                LocationMachineSet.code_no = id;
                //LocationMachineSet.LocationSetCode = collection["tmp_LocationSetCode"];
                //LocationMachineSet.SerialNo = collection["tmp_SerialNo"];
                LocationMachineSet.start_meter_no = (!String.IsNullOrEmpty(collection["start_meter_no"])) ? SF.GetDouble(collection["start_meter_no"]) : 0;
                LocationMachineSet.last_meter_no = (!String.IsNullOrEmpty(collection["last_meter_no"])) ? SF.GetDouble(collection["last_meter_no"]) : 0;
                LocationMachineSet.total_meter_value = (!String.IsNullOrEmpty(collection["total_meter_value"])) ? SF.GetDouble(collection["total_meter_value"]) : 0;
                LocationMachineSet.contract_start_date = (!String.IsNullOrEmpty(collection["contract_start_date"])) ? collection["contract_start_date"] : _getdate.ToString("dd-MM-yyyy", new System.Globalization.CultureInfo("en-US"));
                LocationMachineSet.remark = collection["remark"];
                LocationMachineSet.UpdateBy = (string)Session["User"];

                //ref_SerialNo 
                bool result = _LocationMachineSetRepo.EditOld(LocationMachineSet);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /LocationMachineSet/Delete/5
 
        public ActionResult Delete(string id)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            LocationMachineSetRepo _LocationMachineSetRepo = new LocationMachineSetRepo();
            TransLocationMachineSetModels TransLocationMachineSet = new TransLocationMachineSetModels();
            TransLocationMachineSet = _LocationMachineSetRepo.GetLocationMachineSetById(id);
            return View(TransLocationMachineSet);
        }

        //
        // POST: /LocationMachineSet/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            if (Session["User"] == null) { return RedirectToAction("LogOn", "Account"); }
            try
            {
                // TODO: Add delete logic here
                LocationMachineSetRepo _LocationMachineSetRepo = new LocationMachineSetRepo();
                LocationMachineSetModels LocationMachineSet = new LocationMachineSetModels();
                LocationMachineSet.code_no = id;
                LocationMachineSet.UpdateBy = (string)Session["User"];
                bool result = _LocationMachineSetRepo.Delete(LocationMachineSet);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
